package com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;

import com.senditcertified.mail.R;

public abstract class BaseFragmentActivity extends FragmentActivity {
	private Fragment mFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				initLayout(), null);
		this.setContentView(viewToLoad);
		initViews();
		initComponents();
		addListener();
	}

	protected abstract void initViews();

	protected abstract int initLayout();

	protected abstract void initComponents();

	protected abstract void addListener();

	/**
	 * 
	 * @param fragment
	 */
	public void setNewPage(Fragment fragment) {
		try {
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.replace(R.id.frame_main, fragment, "currentFragment");
			transaction.commitAllowingStateLoss();
			if (mFragment != null)
				transaction.remove(mFragment);
			mFragment = fragment;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		super.startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}
}
